package bpm.dao.impl.fs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import bpm.domain.fs.Container;
import bpm.domain.fs.File;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class FileMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int i) throws SQLException {
		File file = new File();
		
		file.setId(rs.getString("id"));
		file.setNamespace(rs.getString("namespace"));
		file.setName(rs.getString("name"));
		file.setOriginName(rs.getString("origin_name"));
		file.setCreator(rs.getString("creator"));
		file.setSize(rs.getInt("size"));
		
		java.sql.Date fCreateTime = rs.getDate("create_time");
		if(fCreateTime != null) {
			file.setCreateTime(new Date(fCreateTime.getTime()));
		}
		
		java.sql.Date fLastUpdateTime = rs.getDate("last_update_time");
		if(fLastUpdateTime != null) {
			file.setLastUpdateTime(new Date(fLastUpdateTime.getTime()));
		}
		
		file.setDescription(rs.getString("description"));
		
		if(rs.getInt("container_id") > 0) {
			Container c = new Container();
			c.setId(rs.getInt("c_id"));
			c.setContainer(rs.getString("container"));
			c.setCapacity(rs.getLong("capacity"));
			c.setFree(rs.getLong("free"));
			c.setEnableString(rs.getString("enable"));
			
			java.sql.Date cCreateTime = rs.getDate("c_create_time");
			if(cCreateTime != null) {
				c.setCreateTime(new Date(cCreateTime.getTime()));
			}
			
			java.sql.Date cLastUpdateTime = rs.getDate("c_last_update_time");
			if(cLastUpdateTime != null) {
				c.setLastUpdateTime(new Date(cLastUpdateTime.getTime()));
			}
			
			c.setDescription(rs.getString("c_description"));
			
			file.setContainer(c);
		}
		
		return file;
	}

}
